package stack;

import java.util.Stack;

public class StackPractice {
    //                             栈的弹出压入序列
    public boolean IsPopOrder (int[] pushA, int[] popA) {
        //1.先准备一个栈
        Stack<Integer> stack = new Stack<>();
        //2.让 pushA 依次入栈
        //a：每次入栈都判断栈顶元素与 popA
        //b：相等就出栈，然后继续判断条件a
        //c：不相等则回到 a
        int pushIndex = 0;
        int popIndex = 0;
        for (pushIndex = 0; pushIndex < pushA.length; pushIndex++){
            stack.push(pushA[pushIndex]);
            while (!stack.isEmpty() && stack.peek() == popA[popIndex]){
                stack.pop();
                popIndex++;
            }
        }
        //3.最后 pushA 全部入栈后判断 栈是否为空
        if (stack.isEmpty()){
            return true;
        }
        return false;
    }
}
